package logic

import (
	"context"
	"fmt"
	"hire/models/cache"
	"hire/models/mysql"

	"hire/hirerpc/hirerpc"
	"hire/hirerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatesPasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatesPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatesPasswordLogic {
	return &UpdatesPasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdatesPasswordLogic) UpdatesPassword(in *hirerpc.UpdatesPasswordRequest) (*hirerpc.UpdatesPasswordResponse, error) {
	// todo: add your logic here and delete this line
	result, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile).Result()
	if result != in.SmsCode {
		return nil, fmt.Errorf("短信验证码错误，请重新发送")
	}
	var hire mysql.Hire
	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&hire)
	if in.Password != in.Newpassword {
		return nil, fmt.Errorf("密码输入一致,请重新输入")
	}
	if hire.Password == in.Password {
		return nil, fmt.Errorf("两次密码输入不一致")
	}

	hire.Password = in.Password
	return &hirerpc.UpdatesPasswordResponse{
		Id: int64(hire.ID),
	}, nil
}
